package com.holly.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.holly.mapper.EmpExprMapper;
import com.holly.mapper.EmpMapper;
import com.holly.pojo.*;
import com.holly.service.EmpService;
import com.holly.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author holly
 * @create 2025-06-29 16:55
 */
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    //    @Override
//    public PageResultData<Emp> page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin,LocalDate end) {
//        //原始方式实现分页查询
////        //1.获取总记录数
////        Long total = empMapper.count();
////        //2.获取分页查询结果
////        Integer start = (page-1)*pageSize;
////        List<Emp> empList = empMapper.list(start,pageSize);
////
////        return new PageResultData(total,empList);
//        //PageHelper实现分页查询
//
//        //1.设置分页参数
//        PageHelper.startPage(page,pageSize);
//        //2.执行查询
//        List<Emp> empList = empMapper.list(name,gender,begin,end);
//
//        //3.解析查询结果，并封装
//        Page<Emp> p = (Page<Emp>) empList;
//        return new PageResultData<Emp>(p.getTotal(),p.getResult());
//    }
    @Override
    public PageResultData<Emp> page(EmpQueryParam param) {
        //原始方式实现分页查询
//        //1.获取总记录数
//        Long total = empMapper.count();
//        //2.获取分页查询结果
//        Integer start = (page-1)*pageSize;
//        List<Emp> empList = empMapper.list(start,pageSize);
//
//        return new PageResultData(total,empList);
        //PageHelper实现分页查询

        //1.设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());
        //2.执行查询
        List<Emp> empList = empMapper.list(param);

        //3.解析查询结果，并封装
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResultData<Emp>(p.getTotal(), p.getResult());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        //保存员工基本信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        //int i = 1 / 0;
        //保存员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(exprList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getById(Integer id) {
        Emp emp = empMapper.getById(id);
        return emp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        //1.修改员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //2.修改员工工作经历
        //2.1删除员工工作经历（根据员工id）
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //2.2添加员工工作经历
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        System.out.println("exprList:" + exprList);
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }

    }

    @Override
    public List<Emp> list() {
        List<Emp> empList = empMapper.listAllEmp();
        return empList;
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getByUsernameAndPassword(emp);
        if(empLogin != null){
            //生成令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id",empLogin.getId());
            dataMap.put("username",empLogin.getUsername());
            String jwt = JwtUtils.generateToken(dataMap);
            return new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(),jwt);
        }
        return null;
    }

}
